﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Spring.Data.Core;
using System.Data;
using Spring.Data.Common;

namespace TeamTasker.Server.DataAccess.Support
{
    public class SequenceGenerator : AdoDaoSupport
    {
        public long GetNextID()
        {
            // TODO: refactor it to use standard sequences in DB instead of hand-made counter. 
            
            IDbParameters param = AdoTemplate.CreateDbParameters();
            param.AddOut("result", SqlDbType.BigInt);
            int cnt = AdoTemplate.ExecuteNonQuery(CommandType.Text,
                "update PkGen set @result = ID = ID + 1", param);
            if (cnt == 1)
            {
                return (long)param[0].Value;
            }
            else  // most likely, counter is empty after initial db creation
            {
                AdoTemplate.ExecuteNonQuery(CommandType.Text, "insert into PkGen(ID) values(1)");
                return 1;
            }
        }
    }
}
